var express = require('express');
var static = require('express-static');
var mysql = require('mysql');
var http = require('http');
var https = require('https');
var fs = require('fs');
var path = require('path');
var request = require('request');
var bodyParser = require("body-parser");
var db = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : 'xu199412',
  database : 'test'
});
db.connect();
var server=express();
server.use(express.static('music'));
server.use(bodyParser.urlencoded({extended:false}));
server.use(bodyParser.json());
// server.listen(80);
var privateKey  = fs.readFileSync('./hepangzi.top.key', 'utf8');
var certificate = fs.readFileSync('./hepangzi.top.crt', 'utf8');
var credentials = {key: privateKey, cert: certificate};
var httpServer = http.createServer(server);
var httpsServer = https.createServer(credentials, server);
httpServer.listen(80);
httpsServer.listen(443);
server.all('*',function (req, res, next) {
  res.header("Access-Control-Allow-Origin","*");
  res.header('Access-Control-Allow-Credentials', 'true');
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
  res.header("X-Powered-By",' 3.2.1');
  res.header("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
  next();
});
server.get('/gougou.do',function(req,res){
  var json=req.query;
  var aa;
  // http://songsearch.kugou.com/song_search_v2?callback=jQuery112406412366687139484_1513585772055&keyword=%E5%B0%8F%E8%8B%B9%E6%9E%9C
  var url="https://songsearch.kugou.com/song_search_v2?keyword=keywords="+json.keyword;
  https.get(url, (ress) => {
    ress.on('data', (d) => {
      aa+=d;
    });
    ress.on('end', function () {
      res.send(aa);
    })
  }).on('error', (e) => {
    console.error(e);
  })
});
server.get('/gougoua.do',function(req,res){
  var json=req.query;
  var aa;
  // http://m.kugou.com/app/i/getSongInfo.php?cmd=playInfo&hash=90ED34F62293AA8F9A56748F8FFF38A1
  var url="https://www.kugou.com/yy/index.php?r=play/getdata&hash="+json.hash;
  https.get(url, (ress) => {
    ress.on('data', (d) => {
      aa+=d;
    });
    ress.on('end', function () {
      res.send(aa);
      var rega=/undefined/gi;
      var dd=aa.replace(rega,'');
      var jsona=eval('('+dd+')');
      var sqla="INSERT INTO music VALUES (id,'"+jsona.songName+"' , '"+jsona.url+"', '"+json.hash+"');"
      var sqlaa="SELECT * FROM test.`music`";
      db.query(sqla, function (error, results, fields) {

      });
    })
  }).on('error', (e) => {
    console.error(e);
  })
});
server.post("/register",function(req,res){
  var json=req.body;
  var sqla="INSERT INTO userInfo VALUES (uid,'"+json.username+"' , '"+json.password+"');"
  db.query(sqla, function (error, results, fields) {
    if (error==null) {
      res.send({code:0,data:{},msg:"成功"});
      res.end();
    };
  });
})

server.post("/Signin",function(req,res){
  var json=req.body;
  // var sqla="SELECT * FROM test.`userInfo`";
  var sqla="SELECT * FROM test.`userInfo` WHERE NAME='"+json.username+"';"
  db.query(sqla, function (error, results, fields) {
    if (results.length != 0) {
      for (var i = 0; i < results.length; i++) {
        if (json.username != results[i].name || json.password != results[i].password) {
          res.send({code:403,data:{},msg:"账号或者密码错误"});
          res.end();
        }else{
          res.send({code:0,data:{},msg:"成功"});
          res.end();
        }
      };
    }else{
      res.send({code:403,data:{},msg:"账号不存在"});
      res.end();
    }
  });
})

server.post("/userQuery",function(req,res){
  var json=req.body;
  // var sqla="SELECT * FROM test.`userInfo`";
  var sqla="SELECT * FROM test.`userInfo`"
  db.query(sqla, function (error, results, fields) {
    console.log(results)
    if (results.length != 0) {
      res.send({code:0,data: results ,msg:"成功"});
    }
  });
})

server.post("/userdelete",function(req,res){
  var json=req.body;
  // var sqla="SELECT * FROM test.`userInfo`";
  if (json.uid !== ''){
    var sqla="DELETE FROM test.`userInfo` WHERE uid = " + json.uid;
    db.query(sqla, function (error, results, fields) {
      console.log(results)
      if (results.affectedRows != 0) {
        res.send({code:0,data: null ,msg:"删除成功"});
      } else if (results.affectedRows === 0){
        res.send({code:404,data: null ,msg:"uid不存在"});
      }
    });
  } else {
    res.send({code: 401 ,data: null ,msg:"缺少参数"});
  }
})
var data = {
  "code": 200,
  "data": {
    "tableData": [
      {
        "name": "老王",
        "sex": 0,
        "age": 18,
        "score": 60
      },
      {
        "name": "小明",
        "sex": 0,
        "age": 20,
        "score": 20
      },
      {
        "name": "小红",
        "sex": 1,
        "age": 15,
        "score": 100
      }
    ],
    "total": 20
  },
  "msg": "成功"
};
server.post("/testData",function(req,res){
  var json=req.body;
  // var sqla="SELECT * FROM test.`userInfo`";
  if (json.loginName === 'admin' && json.password === 123456){
    res.send(data);
  } else{
    res.send(data);
  }
});
var _json = {
  "msg": "查询成功",
  "code": 200,
  "data": {
    "pageNum": 1,
    "pageSize": 4,
    "size": 4,
    "orderBy": null,
    "startRow": 0,
    "endRow": 3,
    "total": 4,
    "pages": 1,
    "data": [{
      "schoolId": 1879,
      "schoolName": "格林豪泰酒店(天坛店)",
      "list": [{
        "payNumber": 1,
        "vipTypeId": 2
      }, {
        "payNumber": 9,
        "vipTypeId": 3
      }, {
        "payNumber": 7,
        "vipTypeId": 4
      }, {
        "payNumber": 6,
        "vipTypeId": 5
      }],
      "halfYearVip": 0,
      "yearVip": 0,
      "totlePrice": 0.0,
      "proportion": 50.0,
      "shouldPrice": 0.0,
      "state": 0,
      "settlementTime": null
    },{
      "schoolId": 1879,
      "schoolName": "格林豪泰酒店(天坛店)",
      "list": [{
        "payNumber": 1,
        "vipTypeId": 2
      }, {
        "payNumber": 9,
        "vipTypeId": 3
      }, {
        "payNumber": 7,
        "vipTypeId": 4
      }, {
        "payNumber": 6,
        "vipTypeId": 5
      }],
      "halfYearVip": 0,
      "yearVip": 0,
      "totlePrice": 0.0,
      "proportion": 50.0,
      "shouldPrice": 0.0,
      "state": 0,
      "settlementTime": null
    },{
      "schoolId": 1879,
      "schoolName": "格林豪泰酒店(天坛店)",
      "list": [{
        "payNumber": 1,
        "vipTypeId": 2
      }, {
        "payNumber": 9,
        "vipTypeId": 3
      }, {
        "payNumber": 7,
        "vipTypeId": 4
      }, {
        "payNumber": 6,
        "vipTypeId": 5
      }],
      "halfYearVip": 0,
      "yearVip": 0,
      "totlePrice": 0.0,
      "proportion": 50.0,
      "shouldPrice": 0.0,
      "state": 0,
      "settlementTime": null
    }],
    "firstPage": 1,
    "prePage": 0,
    "nextPage": 0,
    "lastPage": 1,
    "isFirstPage": true,
    "isLastPage": true,
    "hasPreviousPage": false,
    "hasNextPage": false,
    "navigatePages": 8,
    "navigatepageNums": [1]
  }
};
var _json1 = {
  "msg": "查询成功",
  "code": 200,
  data: [
    {
      id: 2,
      vipName: "6个月",
    },
    {
      id: 3,
      vipName: "12个月",
    },
    {
      id: 4,
      vipName: "18个月",
    },
    {
      id: 5,
      vipName: "19个月",
    }
  ]
}
server.post('/xiaotong_operation_web/settlement/management',function(req, res) {
  res.send(_json);
  res.end();
});
server.post('/xiaotong_operation_web/settlement/getVipTypeAll',function(req, res) {
  res.send(_json1);
  res.end();
});
